﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace HotelBooking
{
    public class MultiCellBuffer
    {
        public static Semaphore semaphore = new Semaphore(3, 3);
        List<String> orders = new List<String>();
        String processing = String.Empty; 

        //delegate 
        public delegate void OrderToHotelHandler(String order);
        public event OrderToHotelHandler sendOrderToHotel;


        public void receivesString(String order)
        {
            orders.Add(order);
            if (orders.Count>= 3)
            {
                SendOrder(order);
            }
        }

        private void SendOrder(object obj)
        {
            for (int i = 0; i < 3; i++)
            {
                processing = orders[i];
                Thread th = new Thread(new ParameterizedThreadStart(threadMethod));
                th.Start(processing);
               
            }

        }

        private void threadMethod(object processing)
        {
            semaphore.WaitOne();
            Thread.Sleep(200);
            if (processing != null)
            {
                sendOrderToHotel(processing.ToString());
            }
            semaphore.Release();          
            
        }
    }
}
